import { ApiRes, Article, Channel } from '@/types/data'
import { RootAction, RootThunkAction } from '@/types/store'
import { getChannels, setChannels } from '@/utils/channels'
import http from '@/utils/request'
import { hasToken } from '@/utils/token'

export function getUserChannels(): RootThunkAction {
  return async (dispatch) => {
    // 判断有没有登录
    if (hasToken()) {
      const res = await http.get<ApiRes<{ channels: Channel[] }>>(
        '/user/channels'
      )
      // console.log(res.data.data.channels)
      dispatch({
        type: 'home/set_user_channels',
        payload: res.data.data.channels,
      } as RootAction)
    } else {
      // 没登录
      // 先去看本地有无数据, 本地有数据 说明你已经获取过默认数据, 直接拿本地数据
      // 否则 说明你是第一次进入首页(没登录), 去获取默认数据
      const channels = getChannels()
      if (channels.length > 0) {
        // 已经获取过默认数据, 直接拿本地数据
        dispatch({
          type: 'home/set_user_channels',
          payload: channels,
        } as RootAction)
      } else {
        // 是第一次进入首页(没登录), 去获取默认数据
        const res = await http.get<ApiRes<{ channels: Channel[] }>>(
          '/user/channels'
        )

        setChannels(res.data.data.channels)
        dispatch({
          type: 'home/set_user_channels',
          payload: res.data.data.channels,
        } as RootAction)
      }
    }
  }
}

export function getAllChannels(): RootThunkAction {
  return async (dispatch) => {
    const res = await http.get<ApiRes<{ channels: Channel[] }>>('/channels')
    // console.log(res.data.data.channels)
    dispatch({
      type: 'home/set_all_channels',
      payload: res.data.data.channels,
    } as RootAction)
  }
}

export function addChannel(channel: Channel): RootThunkAction {
  return async (dispatch, getState) => {
    const userChannels = getState().home.userChannels
    if (hasToken()) {
      // 登录了 发请求 操作自己的 数据即可
      await http.patch('/user/channels', {
        channels: [channel],
      })
    } else {
      // 未登录 操作本地数据
      setChannels([...userChannels, channel])
    }
    // console.log(res)
    // dispatch(getUserChannels())
    dispatch({
      type: 'home/set_user_channels',
      payload: [...userChannels, channel],
    } as RootAction)
  }
}

export function delChannel(id: number): RootThunkAction {
  return async (dispatch, getState) => {
    const userChannels = getState().home.userChannels
    if (hasToken()) {
      await http.delete('/user/channels/' + id)
    } else {
      // 没登录 需要删除的本地的数据!!
      setChannels(userChannels.filter((item) => item.id !== id))
    }

    dispatch(getUserChannels())
  }
}

export function getArticleList(
  channelId: number,
  timestamp: string
): RootThunkAction {
  return async (dispatch) => {
    const res = await http.get<
      ApiRes<{
        pre_timestamp: string
        results: Article[]
      }>
    >('/articles', {
      params: {
        channel_id: channelId, // 频道id
        timestamp, // 传时间戳 string
      },
    })
    console.log(res.data.data)

    // 更新仓库中某个频道下的文章列表
    dispatch({
      type: 'home/set_channel_articles',
      payload: {
        pre_timestamp: res.data.data.pre_timestamp,
        results: res.data.data.results,
        channel_id: channelId,
      },
    } as RootAction)
  }
}

export function getRefreshList(
  channelId: number,
  timestamp: string
): RootThunkAction {
  return async (dispatch) => {
    const res = await http.get<
      ApiRes<{
        pre_timestamp: string
        results: Article[]
      }>
    >('/articles', {
      params: {
        channel_id: channelId, // 频道id
        timestamp, // 传时间戳 string
      },
    })
    // console.log(res.data.data.results)
    dispatch({
      type: 'home/refresh_list',
      payload: {
        pre_timestamp: res.data.data.pre_timestamp,
        results: res.data.data.results,
        channel_id: channelId,
      },
    } as RootAction)
  }
}
